<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class orderController extends Controller
{
    
    public function postInsert(Request $request){
        if(!session('id')){
            return redirect('/login');
        }
        // 接收传过来的值
        $data = $request->only(['goods_id','goods_num','goods_money','color','size']);
        // dd($data);
        $addr_id = $request->input('addr_id');
        if(!$addr_id){
            
            return redirect('/cart');
        }
        $ouse_id = session('id');
        // dd($data);
        // $order_num = time().rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        // dd($order_num);
        // dd($ouse_id);
        $menu = HomeListController::menu(0);
        // dd($data);
        $total=null;
        $tem = [];
       foreach ($data as $k => $v) {
           foreach($v as $kk=>$vv){
                $tem[$kk][$k] = $v[$kk];
           }
        
        }
        // dd($tem);
        foreach($tem as $k=>$v){
            $tem[$k]['addr_id'] = $addr_id;
            $tem[$k]['ouse_id'] = $ouse_id;
            $tem[$k]['mtime'] = date('Y-m-d H:i:s', time());
            $tem[$k]['order_num'] = time().rand(0,9).rand(0,9).rand(0,9).rand(0,9).rand(0,9);
        }
       // dd($tem);
        foreach($tem as $k=>$v){
            // dd($v);
            $total += $v['goods_money'];
            $res = DB::table('order')->insert($v);
        }
        // dd($total);
        if($res){
            session()->forget('cart');
            return view('home.order.order',['tem'=>$tem,'total'=>$total,'menu'=>$menu]);
        }else{ 
            
            return back();
        }


    }

    // 订单列表页的支付
    public function getPay(Request $request)
    {
        $pay = $request->only(['id','money','goods_id']);
        $menu = HomeListController::menu(0);

        // dd($pay);
        return view('home.order.pay',['pay'=>$pay,'menu'=>$menu]);
    }

    // 购物车处的支付
    public function postPay(Request $request)
    {
          if(!session('id')){
            return redirect('/login');
        }
        $pay = $request->only(['order_num']);
        $money = $request->only(['money']);
        // dd($pay);
        // dd($money);
        $menu = HomeListController::menu(0);
        $tem = [];
        foreach ($pay as $k => $v) {
            foreach ($v as $kk => $vv){
                $tem[$kk][$k] = $v[$kk];
            }
        }
        // dd($tem);
        $data = [];
        foreach ($tem as $k => $v) {
            $data[] = DB::table('order')->where('order_num',$v['order_num'])->first();
        }
        // dd($data);
        $money = $money['money'];
        // dd($money);
        return view('home.order.ppay',['pay'=>$data,'money'=>$money,'menu'=>$menu]);
    }



    public function getOrderlist()
    {
          if(!session('id')){
            return redirect('/login');
        }
        $ouse_id = session('id');
        // dd($ause_id);
        $menu = HomeListController::menu(0);
        $order = DB::table('order')
        ->join('goods','goods.id','=','order.goods_id')
        ->select('order.*','order.id as oid','goods.*')
        ->where('order.ouse_id',$ouse_id)
        ->paginate(5);
        // dd($order);
        

        return view('home.order.orderlist',['order'=>$order,'menu'=>$menu]);
    }
    
    public function getQorder($id)
    {
          if(!session('id')){
            return redirect('/login');
        }
        // // dd($id);
        // $data = [];
        // $date['pay'] = 4
        $res = DB::table('order')->where('id',$id)->update(['pay'=>6]);
        if($res){
            return redirect('/order/orderlist');
        }
    }

    // 执行支付
    public function postAccount(Request $request)
    {
          if(!session('id')){
            return redirect('/login');
        }
        // $book = $request->only(['account','password','id','money','goods_id']);
        // dd($book);
        $goods_id = $request->only(['goods_id']);
        $apassword = $request->only(['password']);
        $amoney = $request->only(['money']);
        $oid = $request->only(['id']);
        $auseid = session('id');
        // dd($goods_id);
        $money = $amoney['money'];
        // dd($money);
        // 通过商品id查找到goods表中的店铺id和老板id
        $gid = DB::table('goods')
                ->join('market','goods.market_id','=','market.id')
                ->join('user','market.use_id','=','user.id')
                ->select('user.id')
                ->where('goods.id',$goods_id)
                ->first();
                // dd($gid->id);
                // dd($auseid);

                
        // 开启事物
            DB::begintransaction();
            $cres = DB::update('update user set account = account-'.$money.' where id = '.$auseid);
            if(!$cres){
                DB::rollBack();//事物回滚
                dd('事物回滚');
            }

            $gres = DB::update('update user set account = account +'.$money.' where id = '.$gid->id);
            if(!$gres){
                DB::rollBack();//事物回滚
                dd('事物回滚');
            }

            if($cres && $gres){
                DB::commit();
            }
            $menu = HomeListController::menu(0);

            $spay = DB::table('order')->where('id',$oid)->update(['pay'=>2,'ptime'=>date('Y-m-d H:i:s', time())]);
            $link = DB::table('friend')->get();
            // dd($link);

            return view('home.order.success',['link'=>$link,'menu'=>$menu]);
            // dd('成功');
    }

        // 订单执行支付
     public function postPaccount(Request $request)
    {
          if(!session('id')){
            return redirect('/login');
        }
        // $book = $request->only(['account','password','id','money','goods_id']);
        // dd($book);
        $pay= $request->only(['goods_id','goods_money','id']);
        $apassword = $request->only(['password']);
        $amoney = $request->only(['goods_money']);
        $auseid = session('id');
        // dd($pay);
        $menu = HomeListController::menu(0);
       $tem = [];
        foreach ($pay as $k => $v) {
            foreach ($v as $kk => $vv){
                $tem[$kk][$k] = $v[$kk];
            }
        }
        // dd($tempnam(dir, prefix)m);
        $gid = [];
        foreach ($tem as $k => $v) {
            // 获取老板id
            $gid = DB::table('goods')
                    ->join('market','goods.market_id','=','market.id')
                    ->join('user','market.use_id','=','user.id')
                    ->select('user.id')
                    ->where('goods.id',$v['goods_id'])
                    ->first();
                    // dd($gid);
            // 开启事物
            DB::begintransaction();
            $cres = DB::update('update user set account = account-'.$v['goods_money'].' where id = '.$auseid);
            if(!$cres){
                DB::rollBack();//事物回滚
                dd('事物回滚');
            }

            $gres = DB::update('update user set account = account +'.$v['goods_money'].' where id = '.$gid->id);
            if(!$gres){
                DB::rollBack();//事物回滚
                dd('事物回滚');
            }

            if($cres && $gres){
                DB::commit();
            $spay = DB::table('order')->where('id',$v['id'])->update(['pay'=>2,'ptime'=>date('Y-m-d H:i:s', time())]);
            }
        }
       
            $link = DB::table('friend')->get();
            // dd($link);

            return view('home.order.success',['link'=>$link,'menu'=>$menu]);
            // dd('成功');
    }
    // 确认收货
     public function getGgoods(Request $request)
    {

          if(!session('id')){
            return view('home.login.login');
        }
        $id = $request->input('id');
        // // dd($id);
        // $data = [];
        // $date['pay'] = 4
        $res = DB::table('order')->where('id',$id)->update(['pay'=>4,'ctime'=>date('Y-m-d H:i:s', time())]);
        if($res){
            return redirect('/order/orderlist');
        }
    }

     // 订单详情
        public function getOdetail(Request $request)
        {   
            $menu = HomeListController::menu(0);
            $id = $request->input('id');
            // 获取老板id
             $data = DB::table('order')
                    ->join('goods','goods.id','=','order.goods_id')
                    ->join('market','goods.market_id','=','market.id')
                    ->join('user','market.use_id','=','user.id')
                    ->join('addr','order.addr_id','=','addr.id')
                    ->select('order.*','user.username','user.phone as uphone','user.email','goods.goods_name',
                        'goods.goods_price','addr.aname','addr.phone','addr.sheng','addr.shi',
                        'addr.xian','addr.info','addr.posid','goods.goods_pic','market.mar_name','market.mar_pic')
                    ->where('order.id',$id)
                    ->first();
                    // dd($id);
                    // dd($data);
            return view('home.order.orderdetail',['data'=>$data,'menu'=>$menu]);

        } 
}
